/*
 * Copyright FUOC.  All rights reserved.

 */
package managedbean;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import jpa.CustomerJPA;
import jpa.OrderJPA;
import jpa.OrderdetailJPA;
import jpa.ShippingAddressJPA;
import ejb.CustomerFacadeRemote;
import ejb.OrderFacadeRemote;
import jpa.ShoppingCart;

/**
 * Managed Bean ShowPetMBean
 */
@ManagedBean(name = "neworder")
@SessionScoped
public class NewOrderMBean implements Serializable{
	
	private static final long serialVersionUID = 1L;	
	
	@EJB
	private OrderFacadeRemote orderFR;

	private ShippingAddressJPA shippingaddress=new ShippingAddressJPA();
	private ShoppingCart shoppingCart = new ShoppingCart();
	private OrderJPA order;
	private CustomerJPA customer=new CustomerJPA();
		
	public NewOrderMBean()  
	{
	}
	
	public NewOrderMBean(ShoppingCart shoopingCart) throws Exception  
	{
		this.shoppingCart=shoopingCart;
		customer=getCustomer();
	}


	public NewOrderMBean(ShoppingCart shoopingCart, ShippingAddressJPA shippingAdress) throws Exception  
	{
		this.shoppingCart=shoopingCart;
		this.shippingaddress=shippingAdress;
		customer=getCustomer();
	}

	public String Customer() throws Exception{
		FacesContext fc = FacesContext.getCurrentInstance();
		ShoppingCart cart = (ShoppingCart)fc.getExternalContext().getSessionMap().get("shoppingCart");
		customer=getCustomer();
		cart.setCustomer(customer);
		return "NewOrderView";	
	}
	
	public CustomerJPA getCustomer() throws Exception
	{		
		//Client al que es creara la comanda, caldra sustituir aquesta part pel dni de l'usuari connectat
		CustomerJPA customer=(CustomerJPA)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
		return customer;
	}
	
	public ShippingAddressJPA getShippingaddress() {
		return shippingaddress;
	}

	//Obtencio de les dades de la pantalla
	public String getAddress(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("address");
 	}
	public String getCity(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("city");
 	}
	public String getPostalCode(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return params.get("postalCode");
 	}
	public ShippingAddressJPA getIdOrder(FacesContext fc){
 		Map<String, Object> params = fc.getExternalContext().getSessionMap();
 		return (ShippingAddressJPA) params.get("idOrder");
 	}
	
	/**Funcio que comprova si el client te o no direccio d'enviament a la seva fitxa
	   Si te direccio i es igual a l'informada es crea l'ordre sense necessitat d'indicar
	   a la funcio l'adreca. Si l'adreca es canvia o no te cap adreca a la fitxa es recuperen les 
	   dades de la pantalla i es crea l'ordre indicant la cistella i adreca
	**/
	public String Different () throws Exception 
	{
		FacesContext fc = FacesContext.getCurrentInstance();
		this.shoppingCart = (ShoppingCart)fc.getExternalContext().getSessionMap().get("shoppingCart");
		this.customer=(CustomerJPA)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginController");
		
		
		if (this.customer.getShippingAddress()!=null) {
			
			shippingaddress=this.customer.getShippingAddress();
		
			if (this.shippingaddress.getAddress().equalsIgnoreCase(this.getAddress(fc)) && 
				this.shippingaddress.getCity().equals(this.getCity(fc)) && 
				this.shippingaddress.getPostalCode().equalsIgnoreCase(this.getPostalCode(fc))){
				NewOrder();
			}
			else
			{
				this.shippingaddress.setAddress(this.getAddress(fc));
				this.shippingaddress.setCity(this.getCity(fc));
				this.shippingaddress.setPostalCode(this.getPostalCode(fc));
				NewOrderNewAddress();
			}
		}
		else
		{
			this.shippingaddress.setAddress(this.getAddress(fc));
			this.shippingaddress.setCity(this.getCity(fc));
			this.shippingaddress.setPostalCode(this.getPostalCode(fc));
			NewOrderNewAddress();
			
		}
	
		return "ConsolaComandes";
	}
		
	@SuppressWarnings("unchecked")
	public void NewOrder() throws Exception
	{	
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		orderFR = (OrderFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/OrderFacadeBean!ejb.OrderFacadeRemote");
		order=(OrderJPA)orderFR.order(this.shoppingCart);
	}
	public void NewOrderNewAddress() throws Exception
	{	
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		orderFR = (OrderFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/OrderFacadeBean!ejb.OrderFacadeRemote");
		order=(OrderJPA)orderFR.order(shoppingCart, shippingaddress);
	}

	public ShoppingCart getshoppingCart() {
		return shoppingCart;
	}


	
}

